/*************************************init*****************************************************/
function query_temp(page) {
	var parm = $('#queryForm').serialize() + sys_get_page(page);
	var url = create_url('temp-query');
	sys_query_parm = parm;
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#tempResultList').html(result);
		}
	});
}

//屏蔽
function shield_temp(code, name){
	$.Zebra_Dialog('确认要屏蔽模板' + name + ' 吗？',{
		'type': 'question','title': '警告' ,'buttons':[
		  {caption:'屏蔽',callback:function() {
			  var url = create_url('temp-shield');
			  aidExecute_temp(code, url);
		  }},
		{caption:'取消',callback:function(){}
		}]
	});
}

//解除屏蔽
function unshield_temp(code, name){
	$.Zebra_Dialog('确认要解除模板' + name + ' 吗？',{
		'type': 'question','title': '警告' ,'buttons':[
		  {caption:'屏蔽',callback:function() {
			  var url = create_url('temp-unshield');
			  aidExecute_temp(code, url);
		  }},
		{caption:'取消',callback:function(){}
		}]
	});
}

function aidExecute_temp(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功');
				query_temp();
			} else {
				sys_msg('操作失败', '操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}
/*************edit.html**************/
//添加行
function addRow_temp(){
	var index = $("#index").val();
	var item = $("#cloneTable").find("tr.rep").clone(true);
	item.find("td:eq(0) > select:eq(0)").attr({"name":"list[" + index + "].type"}).end()
	.find("td:eq(1) > select:eq(0)").attr({"name":"list[" + index + "].lc","i":index,"id":"select_" + index}).end()
	.find("td:eq(2) > input:text:eq(0)").attr({"name":"list[" + index + "].num"}).end()
	.find("td:eq(3) > input:text:eq(0)").attr({"name":"list[" + index + "].point","id":"sore_" + index});
	$("#tby").append(item);
	$("#index").val(parseInt(index) + 1);
}
//删除行
function delRow_temp(obj){
	if ($('#tby').find('tr.rep').length > 1) {
		obj.parents('.rep').remove();
	} else {
		sys_msg('至少保留一行!');
		return false;
	}
	totalInt_temp();
}
//保存
function save_temp(){
	if(checkSave_temp()){
		return;
	}
	var parm = $('#editTempForm').serialize();
	var url = create_url('temp-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_msg('操作成功!');
				sys_load_handle('temp');
			} else {
				sys_msg('操作失败!');
			}
		}
	});
}

//计算总分
function totalInt_temp(){
	var rows = $("#tby").find("tr.rep");
	var total = 0;
	var zf = 0;
	rows.each(function(){
		var num = $(this).find("td:eq(2) > input:text:eq(0)").val();
		var point = $(this).find("td:eq(3) > input:text:eq(0)").val();
		if(!/^\s*\d+\s*$/.test(num)){
			num = 0;
		}
		if(!/\s*\d+\s*/.test(point)){
			point = 0;
		}
		total = num * point;
		zf += total;
	});
	$("#zf").html(zf);
	$("#score").val(zf);
}

//保存前检查
function checkSave_temp(){
	var falg = false;
	var name = $("#name").val();
	if(name == '' || name == null){
		sys_msg('请填写模板名称');
		return true;
	}
	var rows = $("#tby").find("tr.rep");
	if(rows.length == 0){
		sys_msg('请添加模板题目信息!');
		return true;
	}
	rows.each(function(){
		var num = $(this).find("td:eq(2) > input:text:eq(0)").val();
		var point = $(this).find("td:eq(3) > input:text:eq(0)").val();
		if(!/^\s*\d+\s*$/.test(num)){
			sys_msg('数量不能为空或格式填写错误!');
			falg = true;
			return;
		}
		if(!/\s*\d+\s*/.test(point)){
			sys_msg('分值不能为空或格式填写错误!');
			falg = true;
			return;
		}
	});
	return falg;
}

function sore_temp(obj){
	var i =obj.attr("i");
	console.log(i);
	var sore = jQuery("#select_"+ i +"  option:selected").attr("date-sore");
	console.log(sore);
	$("#sore_" + i).val(sore);
}